package com.example.test.dialogfactory;

import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.ColorFilter;
import android.graphics.Paint;
import android.graphics.PixelFormat;
import android.graphics.PorterDuff.Mode;
import android.graphics.PorterDuffXfermode;
import android.graphics.Rect;
import android.graphics.RectF;
import android.graphics.drawable.Drawable;

/**
 * @author Liujin 2019/4/9:8:54:28
 */
public class ClipCoverDrawable extends Drawable {

    private Paint mPaint;
    private RectF mRectF;

    public ClipCoverDrawable() {

        mPaint = new Paint( Paint.ANTI_ALIAS_FLAG );
        mRectF = new RectF();
    }

    @Override
    public void draw( Canvas canvas ) {

        Rect bounds = getBounds();
        int width = bounds.width();
        int height = bounds.height();
        mRectF.set( 0, 0, width, height );

        Bitmap dst = Bitmap.createBitmap( width, height, Config.ARGB_8888 );
        Canvas canvasTemp = new Canvas( dst );
        canvasTemp.drawColor( Color.parseColor( "#FF03DAC5" ) );
        Bitmap src = Bitmap.createBitmap( 100, 400, Config.ARGB_8888 );

        int layer = canvas.saveLayer( mRectF, mPaint, Canvas.ALL_SAVE_FLAG );
        canvas.drawBitmap( dst, 0, 0, mPaint );
        mPaint.setXfermode( new PorterDuffXfermode( Mode.CLEAR ) );
        canvas.drawBitmap( src, 100, 100, mPaint );
        canvas.restoreToCount( layer );
    }

    @Override
    public void setAlpha( int alpha ) {

        mPaint.setAlpha( alpha );
    }

    @Override
    public void setColorFilter( ColorFilter colorFilter ) {

        mPaint.setColorFilter( colorFilter );
    }

    @Override
    public int getOpacity() {

        return PixelFormat.TRANSPARENT;
    }
}
